package com.yy.young.travel.service.impl;

import com.yy.young.common.util.StringUtils;
import com.yy.young.travel.model.SourceGrade;
import com.yy.young.travel.service.ISourceGradeService;
import com.yy.young.travel.service.ISourceService;
import com.yy.young.common.util.CommonUtil;
import com.yy.young.dal.service.IDataAccessService;
import com.yy.young.dal.util.Page;
import com.yy.young.travel.model.Source;
import com.yy.young.travel.util.TravelConstants;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.List;

/**
 * 来源服务实现
 * Created by hua on 2018-03-21.
 */
@Service("sourceService")
public class SourceServiceImpl implements ISourceService {

    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    @Resource(name="sourceGradeService")
    ISourceGradeService sourceGradeService;

    private static final Logger logger = LoggerFactory.getLogger(SourceServiceImpl.class);

    //获取数据列表
    @Override
    public List<Source> getSourceList(Source source) throws Exception {
        return dataAccessService.getList(TravelConstants.MAPPER.TRAVEL_SOURCE + ".getSourceList", source);
    }

    //获取数据列表(分页)
    @Override
    public List<Source> getSourcePage(Source source, Page page) throws Exception {
        return dataAccessService.getList(TravelConstants.MAPPER.TRAVEL_SOURCE + ".getSourceList", source, page);
    }

    //查询单条
    @Override
    public Source getSource(String id) throws Exception {
        return (Source)dataAccessService.getObject(TravelConstants.MAPPER.TRAVEL_SOURCE + ".getSourceById", id);
    }

    //修改
    @Override
    public int updateSource(Source source) throws Exception {
        return dataAccessService.update(TravelConstants.MAPPER.TRAVEL_SOURCE + ".update", source);
    }

    @Override
    public int updateSourceAndGrade(Source source, List<SourceGrade> list) throws Exception {
        //修改资源
        this.updateSource(source);
        //刷新得分
        sourceGradeService.refreshSourceGrade(list, source.getId());
        return 1;
    }

    //批量删除
    @Override
    public int deleteSource(String[] idArr) throws Exception {
        int i = 0;
        for(String id : idArr){
            i += this.deleteSource(id);
        }
        return i;
    }

    //删除单条
    @Override
    public int deleteSource(String id) throws Exception {
        //删除得分数据
        sourceGradeService.deleteSourceGradeBySourceId(id);
        return dataAccessService.delete(TravelConstants.MAPPER.TRAVEL_SOURCE + ".delete", id);
    }

    //插入
    @Override
    public int insertSource(Source source) throws Exception {
        if (StringUtils.isBlank(source.getId())){
            source.setId(CommonUtil.getUUID());
        }
        return dataAccessService.insert(TravelConstants.MAPPER.TRAVEL_SOURCE + ".insert", source);
    }

    @Override
    public int insertSourceAndGrade(Source source, List<SourceGrade> list) throws Exception {
        //插入资源
        this.insertSource(source);
        //插入得分
        sourceGradeService.refreshSourceGrade(list, source.getId());
        return 1;
    }

}